package com.shouhuobao.take.verify;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.camera.AppCameraActivity;
import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.app.Intents;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;
import com.shouhuobao.take.model.StationDetailBean;
import com.shouhuobao.take.model.StationOpenInfoBean;

import droid.frame.utils.GlideUtil;
import droid.frame.utils.http.BitmapUtils;

/**
 * 注册、审核第2步
 *
 * @author coffee<br>
 *         2017年6月26日下午2:10:37
 */
public class RegVerifyActivity2 extends BaseActivity2 implements OnClickListener {

    private Button mSubmit;
    private EditText mStoreName, mStoreWeixin, mStoreArea;// 小区名字、联系人微信号、营业面积
    private TextView mStoreTime;//营业时间
    private ImageView mStationInner, mStationOuter;// 驿站照片
    private StationOpenInfoBean stationBean = null;
    private String stationPic1, stationPic2;

    // 保存2张图片的路径
    private String[] pics = new String[2];
    private boolean isFinish = true;

    @Override
    protected void findViewById() {
        setContentView(R.layout.reg_verify2);
        super.findViewById();
        getAppTitle().setCommonTitle("完善信息");
        mStoreName = (EditText) findViewById(R.id.store_name_edt);
        mStoreTime = (TextView) findViewById(R.id.store_time_edt);
        mStoreWeixin = (EditText) findViewById(R.id.contact_wx_edt);
        mStoreArea = (EditText) findViewById(R.id.store_area_edt);
        mStationInner = (ImageView) findViewById(R.id.inner_image);
        mStationOuter = (ImageView) findViewById(R.id.outer_image);

        findViewById(R.id.inner_image_layout).setOnClickListener(this);
        findViewById(R.id.outer_image_layout).setOnClickListener(this);
        mStoreTime.setOnClickListener(this);

        mSubmit = (Button) findViewById(R.id.submit);
        mSubmit.setOnClickListener(this);
        stationBean = AppCache.getStationOpenInfoBean();
        if (stationBean == null) {
            stationBean = new StationOpenInfoBean();
            AppCache.setStationOpenInfoBean(stationBean);
        }
        //所属小区
        if (isNotEmpty(stationBean.getStationHousingEstateName())) {
            mStoreName.setText(stationBean.getStationHousingEstateName());
            mStoreName.setSelection(stationBean.getStationHousingEstateName().length());
        }
        //营业时间
        if (isNotEmpty(stationBean.getStationOpenTime())) {
            mStoreTime.setText(stationBean.getStationOpenTime());
        }
        //营业面积
        if (isNotEmpty(stationBean.getStationAreaNum())) {
            mStoreArea.setText(stationBean.getStationAreaNum());
            mStoreArea.setSelection(stationBean.getStationAreaNum().length());
        }
        //微信号
        if (isNotEmpty(stationBean.getStationWXNum())) {
            mStoreWeixin.setText(stationBean.getStationWXNum());
            mStoreWeixin.setSelection(stationBean.getStationWXNum().length());
        }
        //店铺内景
        if (isNotEmpty(stationBean.getStationInImagePath())) {
            Bitmap bitmap = BitmapUtils.loadBitmapFromLocal(stationBean.getStationInImagePath());
            if (bitmap != null) {
                stationPic1 = stationBean.getStationInImagePath();
                mStationInner.setImageBitmap(bitmap);
            } else {
                GlideUtil.loadImage(getContext(), stationBean.getStationInImagePath(), 0, mStationInner);
            }
        }
        //店铺外景
        if (isNotEmpty(stationBean.getStationOutImagePath())) {
            Bitmap bitmap = BitmapUtils.loadBitmapFromLocal(stationBean.getStationOutImagePath());
            if (bitmap != null) {
                stationPic2 = stationBean.getStationOutImagePath();
                mStationOuter.setImageBitmap(bitmap);
            } else {
                GlideUtil.loadImage(getContext(), stationBean.getStationOutImagePath(), 0, mStationOuter);
            }
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            //点击选择营业时间
            case R.id.store_time_edt:
                intent = new Intent(getContext(), VerifySelectOpenTimeActivity.class);
                startActivityForResult(intent, 101);
                break;
            //选择店铺内景
            case R.id.inner_image_layout:
                intent = new Intent(getContext(), AppCameraActivity.class);
                intent.setAction(Intents.station_scan_for_shop);
                startActivityForResult(intent, 102);
                break;
            //选择店铺外景
            case R.id.outer_image_layout:
                intent = new Intent(getContext(), AppCameraActivity.class);
                intent.setAction(Intents.station_scan_for_shop);
                startActivityForResult(intent, 103);
                break;
            // 准备提交数据到服务器端
            case R.id.submit:
                //先判断选择的图片是不是都是空，如果都为空，然后判断缓存中有没有
                StationOpenInfoBean infoBean = AppCache.getStationOpenInfoBean();
                if (isNotEmpty(stationPic1) && isNotEmpty(stationPic2)) {
                    pics[0] = stationPic1;
                    pics[1] = stationPic2;
                    showLoadingDialog("上传图片");
                    AppHttp.getInstance().postFils(pics);
                } else {
                    if (infoBean != null) {
                        //说明之前有上传的图片,直接进行提交数据，不需要进行图片上传
                        if (isEmpty(stationPic1) && isEmpty(stationPic2) && isNotEmpty(infoBean.getStationInImagePath()) && isNotEmpty(infoBean.getStationOutImagePath())) {
                            showLoadingDialog("提交数据");
                            AppHttp.getInstance().regVerify(mStoreName.getText().toString(), mStoreTime.getText().toString(),
                                    mStoreArea.getText().toString(), mStoreWeixin.getText().toString(), (infoBean.getStationInImagePath() + "," + infoBean.getStationOutImagePath()).replace("[", "").replace("]", ""));
                        }
                        //修改第一张图片，第二张图片不进行修改
                        else if (isNotEmpty(stationPic1) && isEmpty(stationPic2) && isNotEmpty(infoBean.getStationOutImagePath())) {
                            showLoadingDialog("上传图片");
                            AppHttp.getInstance().postFils(stationPic1);
                        }
                        //修改第二张图片，第一张图片不进行修改
                        else if (isEmpty(stationPic1) && isNotEmpty(stationPic2) && isNotEmpty(infoBean.getStationInImagePath())) {
                            showLoadingDialog("上传图片");
                            AppHttp.getInstance().postFils(stationPic2);
                        } else {
                            showToast("请选择需要上传的图片");
                        }
                    } else {
                        showToast("请选择需要上传的图片");
                    }
                }
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.upload_image:
                cancelLoadingDialog();
                final ReqResult<String> result = JSON.parser(msg.obj, String.class);
                if (checkLoginStatus(result)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            String[] picArrs = result.getResult().split(",");
                            if (picArrs != null) {
                                //两张图片都进行修改
                                if (isNotEmpty(stationPic1) && isNotEmpty(stationPic2)) {
                                    showLoadingDialog("提交数据");
                                    AppHttp.getInstance().regVerify(mStoreName.getText().toString(), mStoreTime.getText().toString(),
                                            mStoreArea.getText().toString(), mStoreWeixin.getText().toString(), (picArrs[0] + "," + picArrs[1]).replace("[", "").replace("]", ""));
                                } else {
                                    StationOpenInfoBean infoBean = AppCache.getStationOpenInfoBean();
                                    if (infoBean != null) {
                                        if (isNotEmpty(stationPic1) && isEmpty(stationPic2) && isNotEmpty(infoBean.getStationOutImagePath())) {
                                            showLoadingDialog("提交数据");
                                            AppHttp.getInstance().regVerify(mStoreName.getText().toString(), mStoreTime.getText().toString(),
                                                    mStoreArea.getText().toString(), mStoreWeixin.getText().toString(), (picArrs[0] + "," + infoBean.getStationOutImagePath()).replace("[", "").replace("]", ""));
                                        } else {
                                            showLoadingDialog("提交数据");
                                            AppHttp.getInstance().regVerify(mStoreName.getText().toString(), mStoreTime.getText().toString(),
                                                    mStoreArea.getText().toString(), mStoreWeixin.getText().toString(), (infoBean.getStationInImagePath() + "," + picArrs[0]).replace("[", "").replace("]", ""));
                                        }
                                    } else {
                                        showToast("图片数据有误");
                                    }
                                }
                            } else {
                                showToast("数据有误");
                            }
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            case MsgID.reg_verify_submit:
                cancelLoadingDialog();
                final ReqResult<?> result2 = JSON.parser(msg.obj);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (checkLoginStatus(result2)) {
                            isFinish = false;
                            AppCache.setStationOpenInfoBean(null);
                            Intent intent = new Intent();
                            StationDetailBean detailBean = new StationDetailBean();
                            detailBean.setCommunity(mStoreName.getText().toString());
                            detailBean.setServiceTime(mStoreTime.getText().toString());
                            detailBean.setArea(mStoreArea.getText().toString());
                            detailBean.setWeixin(mStoreWeixin.getText().toString());
                            if (isNotEmpty(stationPic1) && isNotEmpty(stationPic2)) {
                                detailBean.setStationPics(stationPic1 + "," + stationPic2);
                            } else {
                                StationOpenInfoBean infoBean = AppCache.getStationOpenInfoBean();
                                if (infoBean != null) {
                                    if (isNotEmpty(stationPic1) && isEmpty(stationPic2)) {
                                        detailBean.setStationPics(stationPic1 + "," + infoBean.getStationOutImagePath());
                                    } else {
                                        detailBean.setStationPics(infoBean.getStationInImagePath() + "," + stationPic2);
                                    }
                                } else {
                                    detailBean.setStationPics(stationPic1 + "," + stationPic2);
                                }
                            }
                            intent.putExtra("result", detailBean);
                            setResult(Activity.RESULT_OK, intent);
                            finish();
                        } else {
                            showToast(result2.getMessage());
                        }
                    }
                });
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //选择驿站营业时间
        if (resultCode == Activity.RESULT_OK && requestCode == 101) {
            String result = data.getStringExtra("time");
            if (isNotEmpty(result)) {
                mStoreTime.setText(result);
            }
        }
        // 店铺照片
        else if (resultCode == Activity.RESULT_OK && requestCode == 102) {
            String result = data.getStringExtra("file");
            stationPic1 = result;
            stationBean.setStationInImagePath(stationPic1);
            Bitmap bitmap = BitmapUtils.loadBitmapFromLocal(result);
            if (bitmap != null) {
                mStationInner.setImageBitmap(bitmap);
            }
        }
        // 店铺照片
        else if (resultCode == Activity.RESULT_OK && requestCode == 103) {
            String result = data.getStringExtra("file");
            stationPic2 = result;
            stationBean.setStationOutImagePath(stationPic2);
            Bitmap bitmap = BitmapUtils.loadBitmapFromLocal(result);
            if (bitmap != null) {
                mStationOuter.setImageBitmap(bitmap);
            }
        }
    }

    @Override
    public void finish() {
        if (isFinish) {
            stationBean.setStationHousingEstateName(mStoreName.getText().toString());
            stationBean.setStationOpenTime(mStoreTime.getText().toString());
            stationBean.setStationAreaNum(mStoreArea.getText().toString());
            stationBean.setStationWXNum(mStoreWeixin.getText().toString());
            AppCache.setStationOpenInfoBean(stationBean);
        }
        super.finish();
    }

}
